package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.OrderMapper;
import com.spark.incoicings.dao.ProcuredSupplierMapper;
import com.spark.incoicings.dao.SupplierStatementMapper;
import com.spark.incoicings.service.ProcuredSupplierService;
import com.spark.incoicings.vo.SupplierVo;
import com.spark.invoicings.entity.Order;
import com.spark.invoicings.entity.ProcuredSupplier;
import com.spark.invoicings.entity.SupplierStatement;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ProcuredSupplierServiceImpl
        extends ServiceImpl<ProcuredSupplierMapper, ProcuredSupplier>
        implements ProcuredSupplierService {
    @Resource
    private ProcuredSupplierMapper procuredSupplierMapper;

    @Resource
    private OrderMapper orderMapper;

    /**
     * 根据id获取供应商
     * @param id
     * @return
     */
    @Override
    public ProcuredSupplier selectSupplierById(Integer id) {
        return procuredSupplierMapper.selectById(id);
    }

    /**
     * 保存供应商
     * @param supplier
     * @return
     */
    @Override
    public Result addProcuredSupplier(ProcuredSupplier supplier) {
        // 创建时间
        supplier.setCreateTime(new Date());
        // 修改时间
        supplier.setModifyTime(new Date());
        // 最后活动时间
        supplier.setLastActivityTime(new Date());
        procuredSupplierMapper.insert(supplier);
        return Result.success(200, "success", supplier.getSupplierId());
    }

    /**
     * 根据id删除供应商
     * @param id
     * @return
     */
    @Override
    public boolean delSupplierById(Integer id) {
        // 判断该供应商下是否有订单
        List<Order> orderList = orderMapper.selectById(id);
        if (orderList == null || orderList.isEmpty()) {
            return false;
        }
        return procuredSupplierMapper.deleteById(id) > 0;
    }

    /**
     * 查询所有供应商
     * @param map
     * @return
     */
    @Override
    public List<ProcuredSupplier> selectSupplierAll(Map<String, Object> map) {
        return procuredSupplierMapper.selectSupplierAll(map);
    }


    /**
     * 根据id更新供应商
     * @param supplier
     * @return
     */
    @Transactional
    @Override
    public boolean updateSupplierById(ProcuredSupplier supplier) {
        // 采购总额赋值
        List<Order> orderList = orderMapper.selectById(supplier.getSupplierId());
        double totalPurchaseAmount = orderList.stream().mapToDouble(Order::getTotalAmount).sum();
        supplier.setTotalPurchaseAmount(totalPurchaseAmount);
        // 应付总额
        supplier.setTotalPayableAmount(totalPurchaseAmount);
        // 计算延期率
        double delayRate = 0;
        // 排除除数为零的风险
        if (supplier.getTotalPayableAmount() != 0 && supplier.getUnpaidAmount()!= 0) {
            delayRate = (supplier.getTotalPayableAmount() / supplier.getUnpaidAmount()) * 100;
        }
        supplier.setDelayRate(delayRate);
        return procuredSupplierMapper.updateById(supplier) > 0;
    }
}
